package com.safe.ly_safe.control.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.safe.ly_safe.api.Constants;
import com.safe.ly_safe.base.BasePresenter;
import com.safe.ly_safe.control.model.MainModel;
import com.safe.ly_safe.control.view.MainView;
import com.safe.ly_safe.entity.BaseEntity;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @PACKAGE :com.safe.ly_safe.control.presenter
 * @Author : HSF
 * @Time : On 2022/4/22 16:05
 * @Description : LoginPresenter
 */
public class LoginPresenter extends BasePresenter<MainView<BaseEntity<String>>> {

    private MainModel mainModel;

    public LoginPresenter() {
        this.mainModel = new MainModel();
    }

    public void login(final Context context, final String userName, final String password) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if(!isViewAttached()){
            return;
        }
        mView.showLoading(0);
        Map maps = new HashMap();
        maps.put("userName", userName);
        maps.put("password", password);
        mainModel.login(maps).
                subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Subscriber<BaseEntity<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.onError(Constants.apiLogin,e);
                        mView.hideLoading(0);
                    }

                    @Override
                    public void onNext(BaseEntity<String> result) {

//                        LoginUserData data = result.getData();
//
//                        String sessionId = data.getSessionId();
//                        Log.i("LoginPresenter","sessionId:"+sessionId);
//                        BaseApplication.getInstance().setSessionId(sessionId);
//
//                        SharedUtils.putSingleInfo(context, SharedUtils.LOGIN_USER_NAME,userName);       //登录成功后存储用户名
//                        SharedUtils.putSingleInfo(context, SharedUtils.LOGIN_PASS_WORD,password);       //登录成功后存储登录密码
//                        SharedUtils.putSingleInfo(context, SharedUtils.KEY_HAS_LOGIN_IN,1);     //登录成功后做标记
//
//                        mView.onSuccess(Constants.apiLogin,result);
                        mView.hideLoading(0);
                    }
                });
    }
}
